package oop.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oop.data.Section;
import oop.data.Test;
import oop.db.Database;

public class SectionDAO {

	public static List<Section> fetchById(long id) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT sect_id, sect_text, sect_order, sect_test "
					+ "FROM tblSection " 
					+ "WHERE sect_id = ?";
	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, id);
			rs = stmt.executeQuery();
	
			return fromResultSet(rs, null);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<Section> fetchByTest(Test test) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT sect_id, sect_text, sect_order, sect_test "
					+ "FROM tblSection " 
					+ "WHERE sect_test = ? "
					+ "ORDER BY sect_order";
	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, test.getId());
			rs = stmt.executeQuery();
	
			return fromResultSet(rs, test);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<Section> fromResultSet(ResultSet rs, Test test)
			throws SQLException {
		List<Section> sections = new ArrayList<Section>();
		while (rs.next()) {
			sections.add(new Section(rs.getLong(1), rs.getString(2), rs
					.getInt(3), rs.getInt(4)));
		}
		return sections;
	}

	public static void persist(Section section) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
				String sql = "UPDATE tblSection SET " +
						"sect_text = ?, sect_order = ?, sect_test = ? " +
						"WHERE sect_id = ?";

				conn = Database.get().getConnection();
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, section.getText());
				stmt.setInt(2, section.getOrder());
				stmt.setInt(3, section.getTestId());
				stmt.setLong(4, section.getId());
				stmt.executeUpdate();
		} finally {
			if (rs != null)
				rs.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
	}
	
	public static Section create(String text, int order, int testId) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "INSERT INTO tblSection (" +
					"sect_text, sect_order, sect_test) " +
					"VALUES (?, ?, ?)";

			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, text);
			stmt.setInt(2, order);
			stmt.setInt(3, testId);
			stmt.executeUpdate();
			
			rs = stmt.getGeneratedKeys();
			rs.next();
			long id = rs.getLong(1);
			
			return new Section(id, text, order, testId);
		} finally {
			if (rs != null)
				rs.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
	}
}
